<?php
class Utilerias_model extends CI_Model{
	
	function __construct(){
		parent::__construct();
		$this->load->database();
	}
	
	/**
	 * Agrega la info de una pagina
	 */
	function add($params){
		$data = array(
					'titulo'		=> $params['title'],
					'titulo_limpio'	=> Sys::toAscii($params['title']),
					'contenido'		=> $params['content'],
					'status'		=> $params['status']
				);
		
		$this->db->insert('paginas', $data);
		return array('id_utileria' => $this->db->insert_id());
	}
	
	/**
	 * Obtiene la info de todas las paginas
	 */
	function get($params){	
		$res = $this->db->select("id_pagina as id, titulo as title, titulo_limpio as clean_title, contenido as content, status")->from('paginas')->get();
		$list = array('utils' => $res->result_array());
		return $list;
	}
	
	/**
	 * Obtiene la info de una pagina
	 */
	function get_one($params){
		if($this->exist('paginas', array('id_pagina'=>$params['id_util']))){
			$res = $this->db->select("id_pagina as id, titulo as title, titulo_limpio as clean_title, contenido as content, status")->from('paginas')->where('id_pagina',$params['id_util'])->get();
			$list = array('utils' => $res->result_array());
			return $list;
		}	
		else return FALSE;	
		
	}
	
	/**
	 * Modifica la info de una pagina
	 */
	public function update($params){
		if($this->exist('paginas', array('id_pagina'=>$params['id_util']))){
			$values = array();
						
			if(isset($params['title']))
				$values['titulo'] 		= $params['title'];
			if(isset($params['content']))
				$values['contenido'] 	= $params['content'];
			if(isset($params['status']))
				$values['status'] 		= $params['status'];
			
			$this->db->update('paginas', $values, array('id_pagina' => $params['id_util']));
			
			return array('id' => $params['id_util']);
		}
		return FALSE; //no existe el banner
	}
	
	/**
	 * Elimina una pagina
	 */
	function delete($params){
		if($this->exist('paginas', array('id_pagina'=>$params['id_util']))){
			$this->db->delete('paginas', array('id_pagina' => $params['id_util']));
		}	
		else return FALSE;
	}
	
	private function exist($table, $sql, $return_res=false){
		$res = $this->db->get_where($table, $sql);
		if($res->num_rows() > 0){
			if($return_res)
				return $res->row();
			return TRUE;
		}
		return FALSE;
	}
}